function generateRedPackets(totalAmount, numOfPackets, minAmount, maxAmount) {
    // 初始化结果数组
    var result = [];
    // 计算剩余金额和剩余红包数量
    var remainingAmount = totalAmount;
    var remainingPackets = numOfPackets;

    // 遍历生成每个红包的金额
    for (var i = 0; i < numOfPackets - 1; i++) {
        // 生成一个随机金额，保证剩余金额能够平均分配到剩余的红包中
        var amount = Math.floor(Math.random() * (remainingAmount - remainingPackets * minAmount) / remainingPackets + minAmount);
        result.push(amount);
        remainingAmount -= amount;
        remainingPackets--;
    }
    // 最后一个红包的金额即为剩余的金额
    result.push(remainingAmount);

    // 返回生成的红包金额列表
    return result;
}

// 示例用法
var totalAmount = 1000; // 总金额
var numOfPackets = 10; // 红包数量
var minAmount = 1; // 最小金额
var maxAmount = 200; // 最大金额

var redPackets = generateRedPackets(totalAmount, numOfPackets, minAmount, maxAmount);
console.log(redPackets);
